﻿
@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IEventService _eventService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="600">
    <MDCard>
        <MCardTitle Class="text-h5">
            绑定Steam账号
        </MCardTitle>
        <MCardText>
            绑定后可以同步Steam库存
        </MCardText>

        <MCardText Class="pt-0">
                <MTextField @bind-Value="Model.SteamId" Label="SteamID64（64位的数字Id，可用逗号分隔多个Id）"  />
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Info" Outline Color="primary">
                    Steam个人资料隐私设置<strong>【游戏详情】</strong>需要选择<strong>【公开】</strong>哦
                </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
        </MCardText>
        <MCardActions Class="pb-6 pe-6 ps-6 pt-0">
            <CnGalWebSite.Components.Buttons.MasaButton Text="如何获取SteamId" IsAsync="true" OnClick="OnClickQuestion" Icon="mdi-help-circle-outline" TextStyle />
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="保存" IsAsync="true" OnClick="OnSubmit" Icon="mdi-check" Rounded />
        </MCardActions>
    </MDCard>
</MDialog>

@code {

    [Parameter]
    public EventCallback<string> OnChanged { get; set; }

    public EditUserDataViewModel Model = new EditUserDataViewModel();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    bool Dialog { get; set; }

    bool isReady = false;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            Model = await _httpService.GetAsync<EditUserDataViewModel>("api/space/EditUserData");
            isReady = true;
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取编辑用户信息失败");
        }
    }

    private async Task OnSubmit()
    {
        try
        {
            if (string.IsNullOrWhiteSpace(Model.SteamId))
            {
                await PopupService.ToastErrorAsync("编辑用户Steam信息失败", "SteamId不能为空");
                return;
            }
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^(-?[0-9]*[.]*[0-9]{0,3})$");
            var steamIds = Model.SteamId.Replace("，", ",").Replace("、", ",").Split(',');
            foreach (var item in steamIds)
            {
                if (regex.IsMatch(Model.SteamId) == false)
                {
                    await PopupService.ToastErrorAsync("编辑用户Steam信息失败", "SteamId需为64位纯数字");
                    return;
                }
            }

            var obj = await _httpService.PostAsync<EditUserDataViewModel, Result>("api/space/EditUserData", Model);
            //判断结果
            if (obj.Successful == false)
            {

                await PopupService.ToastErrorAsync("编辑用户Steam信息失败", obj.Error??"参数不符合要求");
            }
            else
            {
                await PopupService.ToastSuccessAsync("编辑用户Steam信息成功", "编辑用户Steam信息成功");
                Dialog = false;
                StateHasChanged();
                await OnChanged.InvokeAsync(Model.SteamId);

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "编辑用户Steam信息失败");
        }
    }

    public void OnClickQuestion()
    {
        _eventService.OpenNewPage(NavigationManager.BaseUri + "articles/index/5");

    }

    public void Refresh(bool dialog)
    {
        Dialog = dialog;
        StateHasChanged();

    }


}
